<template>
	<view><baseForm :column.sync="column" @submitFun="submitFun" @changeColumn="changeColumn" :submitStatus="submitDisable"></baseForm></view>
</template>

<script>
import baseForm from '@/wxcomponents/form/form.vue';
import formMixins from '@/libs/formMixins.js';
import { format } from '@/libs/common.js';
import login from '@/libs/login.js';
import dictMixins from '@/libs/dictMixins.js';
import substituteMixins from './substituteMixins.js';
export default {
	mixins: [formMixins, dictMixins, substituteMixins],
	data() {
		let self = this;
		return {
			afterTimeIndex: '',
			timeout: null,
			column: [
				{
					label: '任教课程',
					name: 'courseCode',
					type: 'picker',
					mode: 'selector',
					required: true,
					disabled: false,
					value: '',
					text: '请选择',

					range: []
				},
				{
					label: '任教班级',
					name: 'classCode',
					type: 'picker',
					mode: 'selector',
					disabled: false,
					required: true,
					value: '',
					text: '请选择',

					range: []
				},
				{
					label: '原授课日期',
					name: 'beforeDate',
					type: 'picker',
					mode: 'date',
					disabled: false,
					required: true,
					value: '',
					start: format('YY-mm-dd', new Date()),
					text: '请选择'
				},

				{
					label: '原授课节次',
					name: 'beforeTimeIndex',
					type: 'checkBox',
					required: true,
					value: '',
					range: []
				},
				{
					label: '代授课程',
					name: 'substituteType',
					type: 'picker',
					mode: 'selector',
					disabled: false,
					required: true,
					value: '',
					text: '请选择',
					range: self.findOneDictList('dict_course_change_substitute')
				},
				{
					label: '代课老师',
					name: 'afterJobNumber',
					type: 'tips',
					singleChoice: true,
					required: true,
					disabled: false,
					value: '',
					text: '',
					url: '/modules/department/departmentMenu',
					originUrl: '/modules/substituteCoruse/changeCoruse',
					chooseItems: []
				},

				{
					label: '代课原因',
					name: 'changeReasonType',
					type: 'picker',
					mode: 'selector',
					disabled: false,
					required: true,
					value: '',
					text: '请选择',
					range: self.findOneDictList('dict_course_change_reasontype')
				},
				{
					label: '',
					name: 'changeReasonDetail',
					type: 'picker',
					mode: 'selector',
					disabled: false,
					required: true,
					value: '',
					text: '请选择',
					range: []
				},
				{
					label: '',
					name: 'changeReasonRemark',
					type: 'normalInput',
					disabled: false,
					// required: true,
					value: '',
					text: '请填写详情',
					hidden: true
				}
			]
		};
	},
	async onLoad() {
		this.$store.state.idsObj = [];
		this.initData();
		let resp = this.findOneDictList('dict_course_change_type');
		// this.EventBus.on('columnItemChange', obj => {
		// 	if (obj.key == 'beforeDate' || obj.key == 'classCode' || obj.key == 'courseCode') {
		// 		debugger
		// 		this.debounce(2000);
		// 		// this.beforeTimeIndexCal();
		// 	}
		// });
		this.EventBus.on('pickerChange', obj => {
			if (obj.key == 'beforeDate' || obj.key == 'classCode' || obj.key == 'courseCode') {
				this.beforeTimeIndexCal();
			}
		});
	},
	onShow(e) {
		this.updateParamsWithCash();
	},
	onHide() {
		this.savePageTempParams({
			name: 'changeTeacher',
			formModel: this.formModel,
			syncColumn: this.syncColumn
		});
	},
	onUnload() {
		this.claearPageTempParams();
		this.EventBus.off('pickerChange',{})
	},
	methods: {
		debounce(time) {
			let that = this;
			// let that = this;
			if (this.timeout) clearTimeout(this.timeout);
			this.timeout = setTimeout(() => {
				that.beforeTimeIndexCal.apply(that, null);
			}, time);
		},
		beforeTimeIndexCal: async function() {
			let that = this;
			let columnObj = this.transColumnToObj(this.column);
			if (columnObj.beforeDate == '' || columnObj.classCode == '' || columnObj.courseCode == '') return;
			let user = await login.getStorage('user');
			let params = {
				jobNumber: user.number,
				courseDate: columnObj.beforeDate,
				classCode: columnObj.classCode,
				courseCode: columnObj.courseCode
			};
			let resp = await that.$http.post({
				url: '/edu/classCourseChange/getClassAndWeekDayIndex',
				data: params
			});
			if (resp.code == 0) {
				that.column[3].range = resp.result.beforeTimeIndex.split(',').map(item => {
					return {
						name: `第${item}节课`,
						value: item
					};
				});
			} else {
				let date = this.column[2].text,
					className = this.column[1].text,
					teacherName = user.name,
					course = this.column[0].text;
				this.showCourseErrModal(date, className, teacherName, course);
			}
		},

		async initData() {
			//任教课程数据
			let rjkc = await this.$http.get({
				url: '/edu/classCourseChange/listCourseByTeacher'
			});
			this.column[0].range = rjkc.result.map(item => {
				return {
					name: item.courseName,
					value: item.courseCode
				};
			});
			//任教班级数据
			let rjbj = await this.$http.get({
				url: '/edu/classCourseChange/listClassByTeacher'
			});
			this.column[1].range = rjbj.result.map(item => {
				return {
					name: item.className,
					value: item.classCode
				};
			});
			//调代收课程
			// let tdskc = await this.$http.get({
			// 	url: '/edu/classCourseChange/listCourseByTeacher'
			// });
			// this.column[6].range = tdskc.result.map(item => {
			// 	return {
			// 		name: item.courseName,
			// 		value: item.courseCode
			// 	};
			// });
		},
		async submitFun() {
			let that = this;
			let user = await login.getStorage('user');
			let params = Object.assign(this.formModel, {
				changeType: 2
			});
			let resp = await this.$http.post({
				url: '/edu/wechat/classCourseChange/save',
				data: params
			});
			if (resp.code == 0) {
				uni.showToast({
					title: '提交成功',
					icon: 'none',
					success() {
						// this.claearPageTempParams();
						uni.navigateBack();
					}
				});
			}
		}
	},
	components: {
		baseForm
	},
	computed: {
		getChangeReasonDetail() {
			let that = this;
			let resp = that.findOneDictList('dict_course_change_reasondetail');
			if (that.column[6].value == 1) {
				let arr = [];
				arr = resp.slice(0, 7);
				that.column[7].range = arr;
				return arr;
			} else if (that.column[6].value == 2) {
				let arr = [];
				arr = resp.slice(7);
				that.column[7].range = arr;
				return arr;
			} else {
				return '';
			}
		},
		leaveReasonName() {
			let column = this.column[7];
			return column.text;
		}
	},
	watch: {
		'$store.state.idsObj': {
			handler(n) {
				this.column[5].chooseItems = this.$store.state.idsObj;
				if (this.column[5].chooseItems.length) {
					this.column[5].value = this.$store.state.idsObj[0].jobNumber;
					this.changeColumn(this.column);
					this.savePageTempParams({
						name: 'changeCoruse',
						formModel: this.formModel,
						syncColumn: this.syncColumn
					});
				}
			},
			deep: true
		},
		column: {
			handler(n, o) {
				if (n[6].value == o[6].value) return;
				this.column[7].value = '';
				this.column[7].text = '请选择';
			},
			deep: true
		},
		leaveReasonName: {
			handler(n) {
				this.column[8].hidden = n != '其他';
			}
		}
	}
};
</script>

<style></style>
